﻿using _11Net8AspNetCore_WebApis.BandModel;
using _11Net8AspNetCore_WebApis.Filter;
using _11Net8AspNetCore_WebApis.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace _11Net8AspNetCore_WebApis.Controller
{
    /// <summary>
    ///  3、表单绑定
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class RouteFormController : ControllerBase
    {
        /// <summary>
        /// 表单不能使用get 和 Patch 请求
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [ServiceFilter(typeof(MyActionAttribute))]
        public string GetRoute([FromForm] int id)
        {
            return id.ToString();
        }

        /// <summary>
        /// 4\json绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/Json")]
        public string GetRouteJson([FromBody] int id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        ///5\ 对象绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/object")]
        public string GetRouteOrder([FromBody] Order id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        ///5\ 对象绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/objectForm")]
        public string GetRouteOrderForm([FromForm] Order id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        ///5\ 对象绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/objectQuery")]
        public string GetRouteOrderQuery([FromQuery] Order id, int age)
        {
            return id.ToString();
        }


        /// <summary>
        ///6\ 对象绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/objects")]
        public string GetRouteOrders([FromBody] List<Order> id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        ///6\ 对象绑定 (传递参数为空)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/objectsForm")]
        public string GetRouteOrderForms([FromForm] List<Order> id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        ///6\ 对象绑定（传递参数为空）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/objectQuerys")]
        public string GetRouteOrderQuerys([FromQuery] List<Order> id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        ///7\ 文件绑定
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{age}/file")]
        public string GetRouteFile(IFormFile id, int age)
        {
            return id.ToString();
        }

        [HttpPost("{age}/files")]
        public string GetRouteFiles(List<IFormFile> id, int age)
        {
            return id.ToString();
        }

        /// <summary>
        /// 8 自定义数据绑定
        /// </summary>
        /// <param name="id"></param>
        /// <param name="age"></param>
        /// <returns></returns>

        [HttpPost("{age}/Cookie")]
        public string GetRouteCookie([ModelBinder(BinderType = typeof(CookieBangModel))]UserModel user, int age)
        {
            return user.ToString();
        }

        /// <summary>
        /// 8 自定义数据绑定
        /// </summary>
        /// <param name="id"></param>
        /// <param name="age"></param>
        /// <returns></returns>

        [HttpPost("{age}/Cookie2")]
        [ServiceFilter(typeof(MyActionFilter))]
        public string GetRouteCookie2([FromCookie] UserModel user, int age)
        {
            return user.ToString();
        }
    }
}
